/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * usuarios_busca.java
 *
 * Created on 27/05/2011, 22:28:27
 */
package Visualização;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fabio
 */
public class usuarios_busca extends javax.swing.JFrame {

    /** Creates new form usuarios_busca */
    public usuarios_busca() {
        initComponents();
        int i;
        this.setLocationRelativeTo(null);
        this.cargoSelect.setEnabled(false);
        nomeInput.setEnabled(false);
        emailInput.setEnabled(false);
        tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
        tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(50);
        DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
        modelo.setNumRows(0); 
        for(i=0;i<1;i++)
            modelo.addRow(new Object [] {"","",""});
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        tituloLabel = new javax.swing.JLabel();
        nomeLabel = new javax.swing.JLabel();
        nomeInput = new javax.swing.JTextField();
        emailLabel = new javax.swing.JLabel();
        matriculaLabel = new javax.swing.JLabel();
        emailInput = new javax.swing.JTextField();
        matriculaInput = new javax.swing.JTextField();
        buscarButton = new javax.swing.JButton();
        cargoLabel = new javax.swing.JLabel();
        cargoSelect = new javax.swing.JComboBox();
        usuariosTable = new javax.swing.JScrollPane();
        tabelaShow = new javax.swing.JTable();
        selecionarButton = new javax.swing.JButton();
        excluirButton = new javax.swing.JButton();
        loginLabel = new javax.swing.JLabel();
        loginInput = new javax.swing.JTextField();
        limparButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable3);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tituloLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        tituloLabel.setText("Buscar Usuários");

        nomeLabel.setText("Nome:");

        emailLabel.setText("E-mail:");

        matriculaLabel.setText("Matrícula:");

        matriculaInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matriculaInputActionPerformed(evt);
            }
        });

        buscarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Buscar.png"))); // NOI18N
        buscarButton.setText("Buscar");
        buscarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarButtonActionPerformed(evt);
            }
        });

        cargoLabel.setText("Cargo:");

        cargoSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "                  ", "Administrador", "Supervisor", "Usuário" }));

        tabelaShow.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Matricula", "Nome", "Login"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaShow.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tabelaShowKeyPressed(evt);
            }
        });
        usuariosTable.setViewportView(tabelaShow);

        selecionarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Escolher.png"))); // NOI18N
        selecionarButton.setText("Selecionar");
        selecionarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selecionarButtonActionPerformed(evt);
            }
        });

        excluirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Lixeira.png"))); // NOI18N
        excluirButton.setText("Excluir");
        excluirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirButtonActionPerformed(evt);
            }
        });

        loginLabel.setText("Login:");

        loginInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginInputActionPerformed(evt);
            }
        });

        limparButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Limpar.png"))); // NOI18N
        limparButton.setText("Limpar Campos");
        limparButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usuariosTable, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                    .addComponent(tituloLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(loginLabel)
                        .addGap(46, 46, 46)
                        .addComponent(loginInput, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(matriculaLabel)
                        .addGap(28, 28, 28)
                        .addComponent(matriculaInput, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nomeLabel)
                        .addGap(44, 44, 44)
                        .addComponent(nomeInput, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(emailLabel)
                        .addGap(43, 43, 43)
                        .addComponent(emailInput, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cargoLabel)
                        .addGap(42, 42, 42)
                        .addComponent(cargoSelect, 0, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buscarButton)
                        .addGap(7, 7, 7)
                        .addComponent(limparButton, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(selecionarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(excluirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loginLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matriculaLabel)
                    .addComponent(matriculaInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nomeLabel)
                    .addComponent(nomeInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cargoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cargoSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarButton)
                    .addComponent(limparButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(usuariosTable, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selecionarButton)
                    .addComponent(excluirButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void excluirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirButtonActionPerformed
        int linha, op;
        op = JOptionPane.showConfirmDialog(null, "Deseja excluir a linha selecionada?", "Excluir", JOptionPane.YES_NO_OPTION);
        if(op==JOptionPane.YES_OPTION)
        {
            String codigo;
            BO.usuarioBO uBO;
            Negocio.usuario u;

            uBO = new BO.usuarioBO();
            linha = tabelaShow.getSelectedRow();
            codigo = tabelaShow.getValueAt(linha, 0).toString();
            uBO.setNomeBO(tabelaShow.getValueAt(linha, 1).toString());

            u = new Negocio.usuario();
            u.Excluir(uBO);
            
            limparButtonActionPerformed(evt);
            tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
            tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
            DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
            modelo.setNumRows(0); 
                modelo.addRow(new Object [] {"","",""});
        }
    }//GEN-LAST:event_excluirButtonActionPerformed

    private void buscarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarButtonActionPerformed
        buscar();
    }//GEN-LAST:event_buscarButtonActionPerformed

    private void loginInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginInputActionPerformed
        buscar();
    }//GEN-LAST:event_loginInputActionPerformed

    private void matriculaInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matriculaInputActionPerformed
        buscar();
    }//GEN-LAST:event_matriculaInputActionPerformed

    private void limparButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparButtonActionPerformed
        loginInput.setText("");
        matriculaInput.setText("");
        nomeInput.setText("");
        emailInput.setText("");
        cargoSelect.setSelectedIndex(0);
}//GEN-LAST:event_limparButtonActionPerformed

    private void selecionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selecionarButtonActionPerformed
        int linha;
        String codigo;
        BO.usuarioBO uBO;

        uBO = new BO.usuarioBO();
        linha = tabelaShow.getSelectedRow();
        codigo = tabelaShow.getValueAt(linha, 0).toString();
        uBO.setLoginBO(tabelaShow.getValueAt(linha, 2).toString());
        new usuarios_alterar_dados(uBO).show();
    }//GEN-LAST:event_selecionarButtonActionPerformed

    private void tabelaShowKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaShowKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) 
        {
            selecionarButtonActionPerformed(null);
        }
    }//GEN-LAST:event_tabelaShowKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new usuarios_busca().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarButton;
    private javax.swing.JLabel cargoLabel;
    private javax.swing.JComboBox cargoSelect;
    private javax.swing.JTextField emailInput;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JButton excluirButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JButton limparButton;
    private javax.swing.JTextField loginInput;
    private javax.swing.JLabel loginLabel;
    private javax.swing.JTextField matriculaInput;
    private javax.swing.JLabel matriculaLabel;
    private javax.swing.JTextField nomeInput;
    private javax.swing.JLabel nomeLabel;
    private javax.swing.JButton selecionarButton;
    private javax.swing.JTable tabelaShow;
    private javax.swing.JLabel tituloLabel;
    private javax.swing.JScrollPane usuariosTable;
    // End of variables declaration//GEN-END:variables

    private void buscar() {
        if(!loginInput.getText().equals(""))
        {
            BO.usuarioBO uBO = new BO.usuarioBO();
            Negocio.usuario p = new Negocio.usuario();
            
            uBO.setLoginBO(loginInput.getText());
            p.BuscarPorLogin(uBO);
            
            nomeInput.setText(uBO.getNomeBO());
            if(String.valueOf(uBO.getMatriculaBO()).equals("0"))
            {
                JOptionPane.showMessageDialog(cargoSelect, "Usuário não encontrado");
                return;
            }
            matriculaInput.setText(String.valueOf(uBO.getMatriculaBO()));
            loginInput.setText(uBO.getLoginBO());
            nomeInput.setText(uBO.getNomeBO());
            emailInput.setText(uBO.getEmailBO());
            cargoSelect.setSelectedIndex(uBO.getCargoBO());
            tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
            tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(50);
            DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
            modelo.setNumRows(0); 
                modelo.addRow(new Object [] {uBO.getMatriculaBO(),uBO.getNomeBO(),uBO.getLoginBO()});
        }
        else if(!matriculaInput.getText().equals(""))
        {
            BO.usuarioBO uBO = new BO.usuarioBO();
            Negocio.usuario p = new Negocio.usuario();
            
            uBO.setMatriculaBO(Integer.parseInt(matriculaInput.getText()));
            p.BuscarPorMatricula(uBO);
            
            matriculaInput.setText(String.valueOf(uBO.getMatriculaBO()));
            if(uBO.getNomeBO().equals(""))
            {
                JOptionPane.showMessageDialog(cargoSelect, "Usuário não encontrado");
                return;
            }
            nomeInput.setText(uBO.getNomeBO());
            loginInput.setText(uBO.getLoginBO());
            emailInput.setText(uBO.getEmailBO());
            cargoSelect.setSelectedIndex(uBO.getCargoBO());
            tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
            tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(50);
            DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
            modelo.setNumRows(0); 
                modelo.addRow(new Object [] {uBO.getMatriculaBO(),uBO.getNomeBO(),uBO.getLoginBO()});
        }
    }
}
